package com.lujianfei.productprice;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.lujianfei.productprice.base.BaseActivity;
import com.lujianfei.productprice.base.CommonAdapter;
import com.lujianfei.productprice.base.ViewHolder;
import com.lujianfei.productprice.ui.main.MainContract;
import com.lujianfei.productprice.ui.main.MainPresenter;
import com.lujianfei.productprice.ui.main.ShopEntity;
import com.lujianfei.productprice.ui.addShop.AddShopActivity;
import com.lujianfei.productprice.ui.order.OrderActivity;
import com.lujianfei.productprice.widget.BottomCart;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;

public class MainActivity extends BaseActivity implements MainContract.View {

    MainContract.Presenter presenter;
    List<ShopEntity> shopEntityList = new ArrayList<>();
    BaseAdapter baseAdapter;
    ListView listview;
    BottomCart bottom_cart;
    ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitleMiddleText("店铺列表",true);
        setTitleLeftImage(R.drawable.ic_order);
        setTitleLeftText("订单");
        showTitleRightImage();
        setTitleRightImage(R.drawable.ic_add);
        listview = findViewById(R.id.listview);
        bottom_cart = findViewById(R.id.bottom_cart);
        progressBar = findViewById(R.id.progressBar);
        presenter = new MainPresenter(this);
        initEvent();
    }

    @Override
    public void onTitleLeftImageClick() {
        startActivity(new Intent(this,OrderActivity.class));
    }

    private void initEvent() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShopEntity itemAtPosition = (ShopEntity) parent.getItemAtPosition(position);
                presenter.showShop(itemAtPosition);
            }
        });
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ShopEntity itemAtPosition = (ShopEntity) parent.getItemAtPosition(position);
                showLongClickDialog(itemAtPosition);
                return true;
            }
        });
    }

    private void showLongClickDialog(final ShopEntity itemAtPosition) {

        final CharSequence[] items = {"编辑", "删除", "取消"};
        new AlertDialog.Builder(this).setTitle("提示").setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                CharSequence item = items[which];
                if ("编辑".equalsIgnoreCase("" + item)) {
                    presenter.openEdit(itemAtPosition);
                } else if ("删除".equalsIgnoreCase("" + item)) {
                    confirmDialog(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            presenter.delete(itemAtPosition);
                        }
                    });
                }
            }}).show();
        }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadData();
        bottom_cart.refreshQuantity();
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        startActivity(new Intent(this,AddShopActivity.class));
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public void setData(List<ShopEntity> list) {
        if (null == baseAdapter){
            baseAdapter = new CommonAdapter<ShopEntity>(this,this.shopEntityList,R.layout.adapter_main_item) {
                @Override
                public void convert(ViewHolder viewHolder, ShopEntity data) {
                    viewHolder.setText(R.id.txtShopName,data.getShopName());
                    viewHolder.setVisible(R.id.txtRemark,!TextUtils.isEmpty(data.getRemark()));
                    viewHolder.setText(R.id.txtRemark,String.format("(%s)",data.getRemark()));
                }
            };
            listview.setAdapter(baseAdapter);
        }
        this.shopEntityList.clear();
        this.shopEntityList.addAll(list);
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(View.GONE);
    }

    private void confirmDialog(final View.OnClickListener onClickListener) {
        new AlertDialog.Builder(this).setTitle("提示").setMessage("确定要删除?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (null!=onClickListener){
                    onClickListener.onClick(null);
                }
            }
        }).setNegativeButton("取消",null).show();
    }
}


